#!/bin/bash

wait=5 # before clearing the screen
#~ toiletdelay=4000
pvdelay=10 # scroll lines (per s?) - more is faster

# fonts chosen by: functionality, readability, beauty
figletfonts=( banner banner3-D banner3 banner4 basic bell big block clb8x10 clb8x8 colossal contrast doh doom epic fender fraktur graffiti hollywood italic jazmine larry3d lcd mini nancyj-fancy nancyj-underlined nancyj ogre os2 pebbles poison puffy rev roman script shadow slant small smscript standard starwars straight thick univers utopiab xbriteb xhelvb xsansb )
# sources:
# ftp://ftp.figlet.org/pub/figlet/fonts/ours.tar.gz
# ftp://ftp.figlet.org/pub/figlet/fonts/contributed.tar.gz
# ftp://ftp.figlet.org/pub/figlet/fonts/international.tar.gz
# ftp://ftp.figlet.org/pub/figlet/fonts/ms-dos.tar.gz
# (maybe a package figlet-fonts exists for your distro?)
# and the fonts that come with figlet 2.2.5

#~ toiletfonts=( $(<./goodtoiletfonts) )
#~ fig=0
#~ toi=0
figmax=${#figletfonts[*]}
#~ toimax=${#toiletfonts[*]}

#~ colarray=( 1 2 3 4 5 6 9 10 11 12 13 14 )
colarray=( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 )
#~ colarray=( 1 2 3 4 5 6 )
colmax=${#colarray[*]}
colchoice=$((RANDOM%colmax))

alignarray=(c l r)
alignchoice=$((RANDOM%3))

tput civis # cursor invisible

while :; do
	lastalignchoice=$alignchoice
	lastcolchoice=$colchoice

	#~ sleep $wait
	#~ xcalib -red 1 0 $((RANDOM%50)) -green 1 0 $((RANDOM%50)) -blue 1 0 $((RANDOM%50)) -a >/dev/null 2>&1
	#~ echo "${toiletfonts[toi]}"
	#~ tput setaf $((RANDOM%7 + 1))
	#~ toilet -I4 -t -W -F gay -f "${toiletfonts[toi]}" "$(date +"%a, %-d.%-m. %R")"

	#~ sleep 0.2
	tput cup $(( RANDOM%($(tput lines) - 5) )) 0
	#~ tput cup $(tput lines) 0

	#~ toilet -t -W -F gay -f "${toiletfonts[toi]}" "$(date +"%a, %-d.%-m. %R") $(date +"%a, %-d.%-m. %R") $(date +"%a, %-d.%-m. %R") $(date +"%a, %-d.%-m. %R")"| pv -l -L $toiletdelay -q
	#~ toi=$((toi + 1))

	tput setaf ${colarray[colchoice]}
	fig="$((RANDOM%figmax))"
	figlet -C utf8 -"${alignarray[alignchoice]}" -t -W -f "${figletfonts[fig]}" "$(date +"%a, %-d.%-m. %R")"| pv -l -L $pvdelay -q
	#~ (( fig < figmax )) && fig=$((fig + 1)) || fig=0

	output=""
	if pidof mocp >/dev/null; then
		x="$(mocp -Q "%state %title" 2>/dev/null)"
		[ "${x%% *}" = "PLAY" ] && output="
MOC:
${x#* }"
	fi
	if pidof deadbeef >/dev/null; then
		x="$(deadbeef --nowplaying-tf "%isplaying% %artist% - %title%" 2>/dev/null)"
		[ "${x%% *}" = "1" ] && output="DeadBeef:
${x#* }$output"
	fi
	if [[ "x$output" != "x" ]]
	then
		#~ sleep 0.5
		until [[ "$colchoice" != "$lastcolchoice" ]]; do colchoice=$((RANDOM%colmax)); done
		lastcolchoice=$colchoice
		echo
		tput setaf ${colarray[colchoice]}
		fig="$((RANDOM%figmax))"
		figlet -C utf8 -"${alignarray[alignchoice]}" -t -W -f "${figletfonts[fig]}" "$output" | pv -l -L $pvdelay -q
		#~ (( fig < figmax )) && fig=$((fig + 1)) || fig=0
	fi

	until [[ "$alignchoice" != "$lastalignchoice" ]]; do alignchoice=$((RANDOM%3)); done
	until [[ "$colchoice" != "$lastcolchoice" ]]; do colchoice=$((RANDOM%colmax)); done
	
	#~ echo "${figletfonts[fig]}" && figlet -I4 <-t -W -f "${figletfonts[fig]}" "$output" && tput cols &&

	#~ (( fig == figmax )) && fig=0
	#~ (( toi == toimax )) && toi=0
	#~ for (( i=0 ; i<$(tput lines) ; i++ ))
	#~ do
		#~ printf "\n"
		#~ sleep 0.02
	#~ done
	sleep $wait
	#~ xcalib -c >/dev/null 2>&1
	clear
done
tput reset
